package cn.slipi.admin.common.interceptor;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lee
 */
public class LogBuilder {

    private static final JsonNodeFactory FACTORY = JsonNodeFactory.instance;

    private ObjectNode node;

    LogBuilder() {
        node = FACTORY.objectNode();
    }

    static LogBuilder builder() {
        return new LogBuilder();
    }

    String build() {
        return node.toString();
    }

    LogBuilder put(String key, Object value) {
        if (value == null || StringUtils.isBlank(value.toString())) {
            return this;
        }
        if (value instanceof JsonNode) {
            node.set(key, (JsonNode) value);
        } else {
            if (StringUtils.isNotBlank(value.toString())) {
                node.put(key, value.toString());
            }
        }
        return this;
    }
}